package com.winchain.chainbill.bill.biz.contract.controller;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.contract.po.ContProt;
import com.shell.bee.contract.service.ContProtService;
import com.shell.bee.file.utils.CommonFileUtil;
import com.shell.bee.mvc.controller.BaseController;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.framework.utils.CommonDataUtil;
import com.winchain.chainbill.bill.biz.contract.service.ContractQueryService;
import com.winchain.chainbill.bill.biz.contract.vo.ContQueryVo;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyContractController extends BaseController {
	@Autowired
	private ContractQueryService contractQueryService;
	@Autowired
	private ContProtService contProtService;

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public List<ContQueryVo> getContBasForPage(PageLimit limit, ContQueryVo contQueryVo) {
		contQueryVo.setCustId(CommonDataUtil.getCurEntId());
		return this.contractQueryService.selectContBasForPage(limit, contQueryVo);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public ContQueryVo getContractDetail(String contId) {
		return this.contractQueryService.selectContractDetail(contId, CommonDataUtil.getCurEntId());
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public void showContract(String protId, HttpServletResponse response) throws Exception {
		ContProt contProt = (ContProt) this.contProtService.selectByKey(protId);
		if (contProt != null) {
			String serverFilePath = contProt.getProtLoc();
			InputStream is = new FileInputStream(serverFilePath);

			response.reset();

			response.addHeader("Content-Disposition", "inline;filename=" + contProt.getProtTitle() + ".pdf");
			response.setContentLength(is.available());
			response.setContentType("application/pdf;charset=UTF-8");

			OutputStream toClient = null;
			try {
				toClient = new BufferedOutputStream(response.getOutputStream());
				int b;
				while ((b = is.read()) != -1) {
					toClient.write(b);
				}
				toClient.flush();
			} catch (Exception e) {
				throw new RuntimeException("������������");
			} finally {
				IOUtils.closeQuietly(is);
				IOUtils.closeQuietly(toClient);
			}
		} else {
			throw new UnCheckMsgException("��������������");
		}
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public void downloadContract(String protId, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ContProt contProt = (ContProt) this.contProtService.selectByKey(protId);
		String serverFilePath = contProt.getProtLoc();
		File file = new File(serverFilePath);
		if (file.exists()) {
			CommonFileUtil.writeFileToClient(contProt.getProtTitle() + ".pdf", new FileInputStream(file), response,
					request);
		} else {
			throw new Exception("����������");
		}
	}
}
